package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalIndustryAttrHelper;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteSpringUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IIndustryService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.KiteIndustryConfig;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

/**
 * 产品线处理类
 * Created by huanghaosheng on 2020/11/12 21:47
 */
@Service
public class ProdLineAttrDealHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneCommonService querySceneCommonService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        if (KiteStringUtils.isEmpty(commonAttr.getValue())) {
            LocalAttrHelper.setSpecialAttrValue(KiteWebContext.getContextNew().getCurrentSceneInstId(), KiteWebContext.getContext().getCustId(), commonAttr, this.getClass().getSimpleName());
        }

        // 如果行业有值则设置产品线下拉值
        KiteAttr indystryAttr = querySceneCommonService.getCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), LocalKeyConsts.INDUSTRY)
            .getResultObject();

        if (Objects.isNull(indystryAttr)) {
            indystryAttr = querySceneCommonService.getCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), LocalKeyConsts.INDUSTRY_NEW)
                .getResultObject();
        }
        if (Objects.nonNull(indystryAttr) && KiteStringUtils.isNotEmpty(indystryAttr.getValue())) {
            //选择行业,联动产品线
            List<KiteAttrValue> productLineAttrList = LocalIndustryAttrHelper.loadProductLine(indystryAttr.getValue());
            commonAttr.setAttrValues(KiteBeanUtils.copyPropsForList(productLineAttrList, StaticAttrDO.class));
        }

        LocalAttrHelper.hidden5GCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), KiteWebContext.getContext().getCustId(), commonAttr);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 产品线
        KiteAttr productLineAttr = querySceneCommonService.getCommonAttr(sceneInstId, fieldName).getResultObject();
        if (KiteStringUtils.isNotEmpty(value) && value.equals(productLineAttr.getValue())) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        // 选定产品线后联动出细分重点产品
        if (KiteStringUtils.isNotEmpty(value)) {
            IIndustryService industryService = KiteSpringUtils.getBeanHighPriority(IIndustryService.class);
            List<KiteIndustryConfig> kiteIndustryList = industryService.listSubIndustry(value);
            if (KiteListUtils.isEmpty(kiteIndustryList)) {
                return super.apply(sceneInstId, fieldName, value, valueDesc);
            }
            List<KiteAttrValue> kiteAttrValues = kiteIndustryList.stream().map(kiteIndustry -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(kiteIndustry.getIndustryCode());
                kiteAttrValue.setAttrValueName(kiteIndustry.getIndustryName());
                return kiteAttrValue;
            }).collect(Collectors.toList());
            // 清空细分重点产品
            KiteAttr keyProductsAttr = querySceneCommonService.getCommonAttr(sceneInstId, LocalKeyConsts.SEGMENT_KEY_PRODUCT)
                .getResultObject();
            if(Objects.isNull(keyProductsAttr)) {
            	keyProductsAttr = querySceneCommonService.getCommonAttr(sceneInstId, LocalKeyConsts.SEGMENT_KEY_PRODUCT_NEW)
                        .getResultObject();
            }
            if (Objects.nonNull(keyProductsAttr)) {
                keyProductsAttr.setValue("");
                keyProductsAttr.setValueDesc("");
                keyProductsAttr.setAttrValues(kiteAttrValues);
                operateSceneCommonService.setCommonAttr(sceneInstId, keyProductsAttr);
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
